package mahjong;

import java.util.ArrayList;
/**
 * Not used in current implementation
 * See MonteCarlo player instead
 */
public class CheatPlayer extends AIPlayer
{
	private static Tile[] first_move;
	
	public Tile[] chooseMove()
	{
		first_move = null;
		int iteration = 0;
		
		while (first_move == null && iteration <= 10)
		{
			findMove(0, iteration);
			iteration++;
		}
		
		if (first_move != null) return first_move;
		
		ArrayList<Tile[]> moves = board.getAvailableMoves();
		return moves.get((int) Math.random() * (moves.size() + 1));
	}
	
	private boolean findMove(int depth, int iteration)
	{
		if (board.isEmpty()) return true;
		if (board.isDeadLock()) return false;
		
		ArrayList<Tile[]> moves = MoveOrderer.sortMoves(board);
		int i = moves.size() > iteration ? iteration : 0;
		
		moves.get(i)[0].removeFromPlay();
		moves.get(i)[1].removeFromPlay();
		
		if (findMove(depth + 1, iteration)) {			
			if (depth == 0) {
				first_move = moves.get(i);
			}
			
			moves.get(i)[0].addToPlay();
			moves.get(i)[1].addToPlay();
			return true;
		}
		moves.get(i)[0].addToPlay();
		moves.get(i)[1].addToPlay();
		return false;
	}
}
